package com.mf.controller;


import com.mf.entity.req.ReqIncomingDetailByIncomingId;
import com.mf.groups.GroupsAddInterface;
import com.mf.groups.GroupsUpdateInterface;
import com.mf.result.Result;
import com.mf.service.ServiceIncomingDetail;
import com.mf.utils.UtilConverter;
import com.mf.entity.req.publicReq.ReqPublicIds;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

    
import com.mf.entity.dto.DtoIncoming;
import com.mf.entity.req.ReqIncoming;
import com.mf.service.ServiceIncoming;
import com.mf.entity.req.search.SearchReqIncoming;

import java.util.List;

/**
 * 
 *
 */
@RestController
@RequestMapping("incoming")
@Tag(name = "来料单")
public class ControllerIncoming {
    @Resource
    private ServiceIncoming serviceincoming;

    @Resource
    private ServiceIncomingDetail serviceincomingDetail;

    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    public Result<Page<DtoIncoming>> findPage(@RequestBody SearchReqIncoming searchReqIncoming) {
        return Result.success(serviceincoming.findPageByParams(searchReqIncoming));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    public Result<List<DtoIncoming>> list(@RequestBody SearchReqIncoming searchReqIncoming) {
        return Result.success(serviceincoming.findListByParams(searchReqIncoming));
    }
    

    @Operation(summary = "保存")
    @PostMapping("/save")
    @Transactional
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqIncomingDetailByIncomingId reqIncoming) {
        serviceincoming.removeById(reqIncoming.getReqIncoming().getId());
        serviceincomingDetail.deleteByIncomingId(reqIncoming.getReqIncoming().getId());
        serviceincoming.save(reqIncoming.getReqIncoming());
        serviceincomingDetail.saveBatch(reqIncoming.getReqIncomingDetail());
        return Result.success();
    }
}
